<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XeO-Schema" targetNamespace="http://www.mpimp-golm.mpg.de/2007/XEML/Environment" elementFormDefault="qualified" xmlns="http://www.mpimp-golm.mpg.de/2007/XEML/Environment" xmlns:mstns="http://www.mpimp-golm.mpg.de/2007/XEML/Environment" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0.0">

  <xs:element name="Repository" type="TRepository">
    <xs:unique name="ParamConstraint">
      <xs:selector xpath="./mstns:Variable" />
      <xs:field xpath="@Name" />
      <xs:field xpath="@Group" />
    </xs:unique>
    <xs:unique name="ParamConstraint2">
      <xs:selector xpath="./mstns:Variable" />
      <xs:field xpath="@TermId" />
    </xs:unique>
    <xs:key name="UnitSourceKey">
      <xs:selector xpath="./mstns:Resources/mstns:UnitSets/mstns:Units" />
      <xs:field xpath="@Name" />
    </xs:key>
    <xs:key name="TypeSourceKey">
      <xs:selector xpath="./mstns:Resources/mstns:TypeSets/mstns:TypeDefine" />
      <xs:field xpath="@Name" />
    </xs:key>
    <xs:key name="ContextEntryKey">
      <xs:selector xpath="./mstns:Resources/mstns:ContextCollection/mstns:Context" />
      <xs:field xpath="@Name" />
    </xs:key>
  </xs:element>
  
  <xs:complexType name="TRepository">
    <xs:sequence>
      <xs:element name="Resources" type="TResources" />
      <xs:element name="Variable" type="TVariable" minOccurs="0" maxOccurs="unbounded">
      </xs:element>
    </xs:sequence>
    <xs:attribute name="NameSpace" type="xs:anyURI" use="required" />
  </xs:complexType>
  
  
  <!--Resources types-->
  <xs:complexType name="TResources">
    <xs:sequence>
      <xs:element name="UnitSets" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Units" type="TNamedUnits" minOccurs="0" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="TypeSets" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="TypeDefine" type="TNamedType" minOccurs="0" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="ContextCollection" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Context" type="TContextEntry" minOccurs="0" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TNamedUnits">
    <xs:complexContent>
      <xs:extension base="TUnits">
        <xs:sequence />
        <xs:attribute name="Name" type="xs:Name" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TNamedType">
    <xs:complexContent>
      <xs:extension base="TTypeDefine">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="Name" type="xs:Name" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TContextEntry">
    <xs:sequence>
      <xs:element name="Description" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="Name" type="xs:Name" />
  </xs:complexType>
  
  <!--Variable types-->
  <xs:complexType name="TVariable">
    <xs:sequence>
      <xs:element name="Definition" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="CommunityUrl" type="xs:anyURI" minOccurs="0" maxOccurs="1" />
      <xs:element name="Context" type="TContext" minOccurs="0" maxOccurs="unbounded">
        <xs:keyref name="contextRef" refer="ContextEntryKey">
          <xs:selector xpath="." />
          <xs:field xpath="@Name">
          </xs:field>
        </xs:keyref>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:token" use="required" />
    <xs:attribute name="Group" type="xs:string" use="required" />
    <xs:attribute name="RecommendationLevel" type="TRecommendationLevel" />
    <xs:attribute name="TermId" type="xs:Name" use="required" />
  </xs:complexType>
  <xs:complexType name="TContext">
    <xs:sequence>
      <xs:choice>
        <xs:element name="TypeDefine" type="TTypeDefine" />
        <xs:element name="TypeSource" type="TTypeSource">
          <xs:keyref name="typeRef" refer="TypeSourceKey">
            <xs:selector xpath="." />
            <xs:field xpath="@typeRef" />
          </xs:keyref>
        </xs:element>
      </xs:choice>
      <xs:choice minOccurs="0" maxOccurs="1">
        <xs:element name="Units" type="TUnits" />
        <xs:element name="UnitsSource" type="TUnitSource">
          <xs:keyref name="unitRef" refer="UnitSourceKey">
            <xs:selector xpath="." />
            <xs:field xpath="@unitRef" />
          </xs:keyref>
        </xs:element>
      </xs:choice>
      <xs:element name="DefaultValue" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="DefaultCycle" type="TCycle" minOccurs="0" maxOccurs="1" />
      <xs:element name="Labels" minOccurs="0" maxOccurs="1">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ValueLabel" type="TValueLabel" minOccurs="0" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:token" use="required" />
  </xs:complexType>
  <xs:complexType name="TTypeDefine">
    <xs:sequence>
      <xs:element name="Min" minOccurs="0" maxOccurs="1" />
      <xs:element name="Max" minOccurs="0" maxOccurs="1" />
      <xs:element name="Enum" minOccurs="0" maxOccurs="unbounded">
      </xs:element>
      <xs:element name="Exclude" minOccurs="0" maxOccurs="unbounded">
      </xs:element>
    </xs:sequence>
    <xs:attribute name="BaseType" type="TBaseType" />
  </xs:complexType>
  <xs:complexType name="TTypeSource">
    <xs:sequence />
    <xs:attribute name="typeRef" type="xs:Name" />
  </xs:complexType>
  <xs:complexType name="TUnits">
    <xs:sequence>
      <xs:element name="Default" type="TDefaultUnit" minOccurs="1" maxOccurs="1" />
      <xs:element name="Unit" type="TUnit" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TDefaultUnit">
    <xs:sequence />
    <xs:attribute name="Name" type="xs:normalizedString" use="required" />
    <xs:attribute name="Symbol" type="xs:token" use="required" />
  </xs:complexType>
  <xs:complexType name="TUnit">
    <xs:complexContent>
      <xs:extension base="TDefaultUnit">
        <xs:sequence />
        <xs:attribute name="Conversion" type="xs:string" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TUnitSource">
    <xs:sequence />
    <xs:attribute name="unitRef" type="xs:Name" />
  </xs:complexType>
  <xs:complexType name="TValueLabel">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="Expression" type="xs:token" use="required" />
    <xs:attribute name="Label" type="xs:token" use="required" />
  </xs:complexType>
   <xs:complexType name="TCycle">
    <xs:sequence>
      <xs:element name="Value" type="TCycleValue" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
   <xs:complexType name="TCycleValue">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Duration" type="TRelativeTime" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  
  <!--Simple types-->
 <xs:simpleType name="TBaseType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Number" />
      <xs:enumeration value="Text" />
      <xs:enumeration value="Boolean" />
    </xs:restriction>
  </xs:simpleType>
   <xs:simpleType name="TRelativeTime">
    <xs:restriction base="xs:normalizedString">
      <xs:pattern value="(([1-9][0-9]*.){0,1}([0-1][0-9]|[2][0-3])(:[0-5][0-9]){1,2})|([0-9]|[1-9][0-9]+)" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="TRecommendationLevel">
    <xs:restriction base="xs:string">
      <xs:enumeration value="1" />
      <xs:enumeration value="2" />
      <xs:enumeration value="3" />
    </xs:restriction>
  </xs:simpleType>  
</xs:schema>